<template>
  <div>
    <a-form id="formLogin" class="user-layout-login" ref="formLogin" :form="form" @submit="handleSubmit">
      <a-form-item>
        <a-input
          size="large"
          type="text"
          placeholder="请输入用户名"
          v-decorator="[
            'username',
            { rules: [{ required: true, message: '请输入用户名' }], validateTrigger: 'change' },
          ]"
        >
          <a-icon slot="prefix" type="user" :style="{ color: '#D6DFEC' }" />
        </a-input>
      </a-form-item>

      <a-form-item>
        <a-input-password
          size="large"
          placeholder="请输入密码"
          v-decorator="['pwd', { rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur' }]"
        >
          <a-icon slot="prefix" type="lock" :style="{ color: '#D6DFEC' }" />
        </a-input-password>
      </a-form-item>
      <a-form-item>
        <a-form-item style="margin-top: 10px">
          <a-button
            size="large"
            type="primary"
            htmlType="submit"
            class="login-button"
            :loading="state.loginBtn"
            :disabled="state.loginBtn"
          >登录</a-button
          >
        </a-form-item>
        <div class="forget">
          <div class="forget-btn" @click="changePassword">忘记密码</div>
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import monitor from '@/utils/monitor'
import avatarDropdown from '@/monitor/views/user/login.js'

// import md5 from 'md5'
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
// import store from '@/store'

const watch = monitor(avatarDropdown)

export default {
  components: {},
  data () {
    return {
      form: this.$form.createForm(this),
      state: {
        loginBtn: false
      }
    }
  },
  created () { },
  methods: watch({
    changePassword () {
      this.$parent.isForget = true
    },
    ...mapActions(['Login', 'ClearProject']),
    // handler
    handleSubmit (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        Login
      } = this

      state.loginBtn = true

      const validateFieldsKey = ['username', 'pwd']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = {}
          loginParams.account = values.username
          // loginParams.pwd = md5(values.password)
          loginParams.pwd = values.pwd
          console.log('login form', loginParams)
          this.$store.dispatch('ClearProject').then(() => {
            Login(loginParams)
              .then(res => this.loginSuccess(res))
              .catch(err => this.requestFailed(err))
              .finally(() => {
                state.loginBtn = false
              })
          })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
      this.clickLogin()
    },
    loginSuccess (res) {
      // 没有认证的情况下跳去认证页
      // res[verified_status] == 0  跳转到 实名
      if (res['isAdmin'] === 0 && res['verified_status'] === 0) {
        this.$router.push({ path: '/person' })
      } else if (res['isRestPwd'] === 0) {
        // 系统用户 且未重置密码
        this.$router.push({ path: '/person', query: { type: 1 } })
      } else {
        this.$router.push({ path: '/' })
        // 延迟 1 秒显示欢迎信息
        setTimeout(() => {
          this.$notification.success({
            message: '欢迎',
            description: `${timeFix()}，欢迎回来`
          })
        }, 1000)
      }
    },
    requestFailed (err) {
      console.log('====登录失败====', err)
    },
    // 登录埋点
    clickLogin () {}
  })
}
</script>

<style lang="less" scoped>
.user-layout-login {
  ::v-deep .ant-input-suffix {
    color: #ABB8CE;
  }
  /* 去除谷歌浏览器中input自动填充背景 */
  ::v-deep input {
    box-shadow: 0 0 0 1000px #fff inset;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
    background-color: #3D92FF;
    border: none;
  }

  .forget {
    font-size: 14px;
    color: #ABB8CE;
    text-align: center;
    position: relative;

    &:after {
      content: '';
      display: block;
      border-top: 1px solid #E5EBF3;
      width: 100%;
      height: 0;
      position: absolute;
      top: 50%;
      left: 0;
      z-index: 3;
    }

    .forget-btn {
      position: relative;
      display: inline-block;
      background: #fff;
      padding: 0 13px;
      z-index: 4;
      cursor: pointer;
    }
  }
}
</style>
